home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / szlibd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.0 KB  |  106 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: szlibd.c,v 1.2.2.1 2000/11/02 15:05:08 igorm Exp $ */
  20. /* zlib decoding (decompression) filter stream */
  21. #include "std.h"
  22. #include "gsmemory.h"
  23. #include "gsmalloc.h"        /* for gs_memory_default */
  24. #include "strimpl.h"
  25. #include "szlibxx.h"
  26.  
  27. /* Initialize the filter. */
  28. private int
  29. s_zlibD_init(stream_state * st)
  30. {
  31.     stream_zlib_state *const ss = (stream_zlib_state *)st;
  32.     int code = s_zlib_alloc_dynamic_state(ss);
  33.  
  34.     if (code < 0)
  35.     return ERRC;    /****** WRONG ******/
  36.     if (inflateInit2(&ss->dynamic->zstate,
  37.              (ss->no_wrapper ? -ss->windowBits : ss->windowBits))
  38.     != Z_OK
  39.     ) {
  40.     s_zlib_free_dynamic_state(ss);
  41.     return ERRC;    /****** WRONG ******/
  42.     }
  43.     st->min_left=1;
  44.     return 0;
  45. }
  46.  
  47. /* Reinitialize the filter. */
  48. private int
  49. s_zlibD_reset(stream_state * st)
  50. {
  51.     stream_zlib_state *const ss = (stream_zlib_state *)st;
  52.  
  53.     if (inflateReset(&ss->dynamic->zstate) != Z_OK)
  54.     return ERRC;    /****** WRONG ******/
  55.     return 0;
  56. }
  57.  
  58. /* Process a buffer */
  59. private int
  60. s_zlibD_process(stream_state * st, stream_cursor_read * pr,
  61.         stream_cursor_write * pw, bool ignore_last)
  62. {
  63.     stream_zlib_state *const ss = (stream_zlib_state *)st;
  64.     z_stream *zs = &ss->dynamic->zstate;
  65.     const byte *p = pr->ptr;
  66.     int status;
  67.  
  68.     /* Detect no input or full output so that we don't get */
  69.     /* a Z_BUF_ERROR return. */
  70.     if (pw->ptr == pw->limit)
  71.     return 1;
  72.     if (pr->ptr == pr->limit)
  73.     return 0;
  74.     zs->next_in = (Bytef *)p + 1;
  75.     zs->avail_in = pr->limit - p;
  76.     zs->next_out = pw->ptr + 1;
  77.     zs->avail_out = pw->limit - pw->ptr;
  78.     status = inflate(zs, Z_PARTIAL_FLUSH);
  79.     pr->ptr = zs->next_in - 1;
  80.     pw->ptr = zs->next_out - 1;
  81.     switch (status) {
  82.     case Z_OK:
  83.         return (pw->ptr == pw->limit ? 1 : pr->ptr > p ? 0 : 1);
  84.     case Z_STREAM_END:
  85.         return EOFC;
  86.     default:
  87.         return ERRC;
  88.     }
  89. }
  90.  
  91. /* Release the stream */
  92. private void
  93. s_zlibD_release(stream_state * st)
  94. {
  95.     stream_zlib_state *const ss = (stream_zlib_state *)st;
  96.  
  97.     inflateEnd(&ss->dynamic->zstate);
  98.     s_zlib_free_dynamic_state(ss);
  99. }
  100.  
  101. /* Stream template */
  102. const stream_template s_zlibD_template = {
  103.     &st_zlib_state, s_zlibD_init, s_zlibD_process, 1, 1, s_zlibD_release,
  104.     s_zlib_set_defaults, s_zlibD_reset
  105. };
  106.